package org.xtb.CS_3;

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {
    private static final String END_FLAG = "###END###";
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 8889);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

            String inputLine;
            while (true) {
                // 显示菜单
                System.out.println("==============================");
                System.out.println("个人通讯录系统");
                System.out.println("==============================");
                System.out.println("请选择操作：");
                System.out.println("1. 创建联系人表");
                System.out.println("2. 添加联系人");
                System.out.println("3. 修改联系人");
                System.out.println("4. 删除联系人");
                System.out.println("5. 查看联系人");
                System.out.println("6. 退出");
                System.out.print("请输入选项号码：");
                int choice = Integer.parseInt(consoleReader.readLine());

                switch (choice) {
                    case 1:
                        // 发送创建联系人表请求
                        out.println("创建联系人表");
                        String response1 = readResponse(in);
                        System.out.println("服务器响应：" + response1);
                        break;
                    case 2:
                        // 发送添加联系人请求
                        System.out.print("请输入联系人信息（姓名,住址,电话）：");
                        String contactData = consoleReader.readLine();
                        out.println("添加联系人:"+contactData);

                        String response2 = readResponse(in);
                        System.out.println("服务器响应：" + response2);
                        break;
                    case 3:
                        // 发送修改联系人请求
                        System.out.print("请输入要修改的联系人id：");
                        String name = consoleReader.readLine();
                        System.out.print("请输入新的联系人信息（姓名,住址,电话）：");
                        contactData = consoleReader.readLine();
                        out.println("修改联系人:"+name + "," + contactData);

                        String response3 = readResponse(in);
                        System.out.println("服务器响应：" + response3);
                        break;
                    case 4:
                        // 发送删除联系人请求
                        System.out.print("请输入要删除的联系人id：");
                        name = consoleReader.readLine();
                        out.println("删除联系人:"+name);

                        String response4 = readResponse(in);
                        System.out.println("服务器响应：" + response4);
                        break;
                    case 5:
                        // 发送查看联系人请求
                        out.println("查看联系人");

                        String response5 = readResponse(in);
                        System.out.println("服务器响应：\n" + response5);
                        break;
                    case 6:
                        System.out.println("退出程序");
                        in.close();
                        out.close();
                        consoleReader.close();
                        socket.close();
                        System.exit(0);
                    default:
                        System.out.println("无效的选项，请重新输入");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String readResponse(BufferedReader in) throws IOException {
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) {
            if (line.equals(END_FLAG)) {
                break;
            }
            response.append(line).append("\n");
        }
        return response.toString().trim();
    }
}
